package cn.itsource.user.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.LogininfoDto;
import cn.itsource.user.domain.User;
import cn.itsource.user.service.ILoginInfoService;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService service;
    @Autowired
    private ILoginInfoService loginInfoService;

    @PostMapping("/checkPhone")
    public AjaxResult yz(@RequestBody User user){
        try {
            service.finduserbypassword(user.getPhone());
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }

    }

    @PostMapping("/regst")
    public AjaxResult  reg(@RequestBody LogininfoDto loginInfodto){
        try {
            System.out.println(loginInfodto.getPassword());
            loginInfoService.addreg(loginInfodto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }
    }
    @PostMapping("/login")
    public  AjaxResult login(@RequestBody LogininfoDto logininfoDto){
        System.out.println(logininfoDto.toString());
        try {

            return  loginInfoService.querybyusernameandtype(logininfoDto);
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }

    }
}
